/***
 * * PROJECT
 * *    ATaG
 * * VERSION
 * *    $Revision: 1.21 $
 * * DATE
 * *    $Date: 2007/01/29 18:44:37 $
 * * LOG
 * *    $Log: NodeInfo.java,v $
 * *    Revision 1.21  2007/01/29 18:44:37  lmottola
 * *    Refactored task assignment code.
 * *
 * *    Revision 1.20  2007/01/29 17:48:01  lmottola
 * *    Added debug info in toString() method.
 * *
 * *    Revision 1.19  2006/12/30 09:26:49  animesh
 * *    fixed minor typo.
 * *
 * *    Revision 1.18  2006/12/20 23:05:07  lmottola
 * *    Removed reference to ATaGProgram in NodeInfo.
 * *
 * *    Revision 1.17  2006/12/14 23:06:35  lmottola
 * *    Added constructor for compiler use.
 * *
 * *    Revision 1.16  2006/12/14 05:15:15  lmottola
 * *    Moved Estimator in atag.compiler and various cleanup.
 * *
 * *    Revision 1.15  2006/11/03 23:09:34  lmottola
 * *    Changed nodeAttributes package.
 * *
 * *    Revision 1.14  2006/11/03 23:08:17  lmottola
 * *    Traffic application beta running.
 * *
 * *    Revision 1.13  2006/10/30 23:53:56  animesh
 * *    added new function to return the value of the regionID.. perhaps this needs more work, as a node may have multiple regionIDs.
 * *
 * *    Revision 1.12  2006/10/27 01:28:28  lmottola
 * *    Debugging support added.
 * *
 * *    Revision 1.11  2006/10/27 00:11:58  lmottola
 * *    Automatic generation of attribute describing the tasks running on a node.
 * *
 * *    Revision 1.10  2006/10/25 22:50:49  lmottola
 * *    Startup file created.
 * *
 * *    Revision 1.9  2006/10/25 19:06:20  lmottola
 * *    Refactored myLocation().
 * *
 * *    Revision 1.8  2006/10/25 18:24:02  lmottola
 * *    Added getMyLocation() method.
 * *
 * *    Revision 1.7  2006/10/24 00:08:50  lmottola
 * *    Added sendingCost field.
 * *
 * *    Revision 1.6  2006/10/21 01:05:12  lmottola
 * *    Changed method getNodeAttributes.
 * *
 * *    Revision 1.5  2006/10/20 00:30:01  lmottola
 * *    Added constructor.
 * *
 * *    Revision 1.4  2006/10/20 00:27:15  lmottola
 * *    Made NodeAttribute abstract.
 * *
 * *    Revision 1.3  2006/10/19 21:49:22  lmottola
 * *    Added getAttributes method.
 * *
 * *    Revision 1.2  2006/10/19 21:21:52  lmottola
 * *    Added CVS preamble.
 * *
 * *
 * *   ATaG (Abstract Task Graph) - Data-driven Macroprogramming for
 * *                                Sensor Networks
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.runtimeForGUI;

import java.util.*;

import atag.runtime.ln.nodeAttributesForGUI.NodeAttribute;
import atag.runtime.ln.nodeAttributesForGUI.SetAttribute;

public class NodeInfo {

	private Vector attributes;

	private Vector assignedTasks;

	private int myId;
        
        private String physicalAddress;

	private NodeLocation myLocation;

	private int sendingCost;

//        public String myAbilityName = new String();
//        public String myAbilityValue = new String();
//        public String myRegionName = new String();
//        public Integer myRID;
	//private IClock clock;

	public static final String ASSIGNED_TASK_ATTR_NAME = "runningTasks";

	// For compiler use 
	public NodeInfo(int myId, String phyAddress, NodeLocation myLocation, Collection nodeAttributes) {
		
		attributes = new Vector(nodeAttributes);
                this.physicalAddress = phyAddress;
		this.myId = myId;
		this.myLocation = myLocation;
		this.assignedTasks = new Vector();
	}
        
        public void setNodeInfo(int myId, String phyAddress, NodeLocation myLocation, Collection nodeAttributes) {
		this.clearNodeAttribute();
		attributes = new Vector(nodeAttributes);
                this.physicalAddress = phyAddress;
		this.myId = myId;
		this.myLocation = myLocation;
		this.assignedTasks = new Vector();
	}

	public void assignTasks(Set tasks) {

		this.assignedTasks = new Vector(tasks);

		// Remove any previously assignedTask attribute
		Iterator it = attributes.iterator();
		while (it.hasNext()) {
			NodeAttribute attr = (NodeAttribute) it.next();
			if (attr.getKey().equals(ASSIGNED_TASK_ATTR_NAME)) {
				attributes.remove(attr);
				break;
			}
		}

		attributes.add(new SetAttribute(ASSIGNED_TASK_ATTR_NAME, assignedTasks));
	}

	// For runtime use
	
        public void clearNodeAttribute() {
            attributes.clear();
        }
        
        public void resetNodeAttribute(Collection nodeAttributes) {
            this.clearNodeAttribute();
            attributes = new Vector(nodeAttributes);
        }

	public void addNodeAttribute(NodeAttribute attr) {
		attributes.add(attr);
	}

	public Collection getNodeAttributes() {
		return attributes;
	}
        
        public Vector getNodeAttributesWithVector() {
		return attributes;
	}

	public Object getAttributeByName(String name) {

		Iterator it = attributes.iterator();
		while (it.hasNext()) {
			NodeAttribute attr = (NodeAttribute) it.next();
			if (attr.getKey().equals(name)) {
                                //System.out.println("found");
                                //System.out.println("Success!"+attr.getKey());
				return attr.getValue();
			}
		}
		return null;
	}

        public ArrayList<String> getAttributeNames() {

            ArrayList<String> attrNames = new ArrayList<String>();
		Iterator it = attributes.iterator();
		while (it.hasNext()) {
			NodeAttribute attr = (NodeAttribute) it.next();
			attrNames.add(attr.getKey());
		}
		return attrNames;
	}
        
	public int getMyId() {
            return this.myId;
	}
        
        public void setMyId(int id) {
            this.myId = id;
	}
        
        public String getPhysicalAddress() {
		return this.physicalAddress;
	}
        
        public void setPhysicalAddress(String newAddress) {
		this.physicalAddress = newAddress;
	}

        public void setLocation(float x, float y) {
		this.myLocation.setX(x);
                this.myLocation.setY(y);
	}

	public NodeLocation getMyLocation() {
		return this.myLocation;
	}

	public boolean isTaskAssigned(int taskID) {
		return assignedTasks.contains(new Integer(taskID));
	}

	public int getSendingCost() {
		return sendingCost;
	}

	/*public IClock getClock() {
		return clock;
	}*/
/*
	public void debugPrint(String debugMessage) {
		System.out.println(myId + ": " + debugMessage + " at time "
				+ clock.getTime() + " " + clock.getDate());
		System.out.flush();
	}*/

    public String toString() {
    	
    	String ret = "NodeInfo id: "+myId;
    	
    	Iterator it = attributes.iterator();
    	while (it.hasNext()) {
    		ret = ret.concat("\nAttribute "+ ((NodeAttribute)it.next()).toString());
    	}
    	
    	return ret;
    }
    
    public boolean doesHaveAttribute(NodeAttribute attr) {

            //ArrayList<String> attrNames = new ArrayList<String>();
   	    Iterator it = attributes.iterator();
            if(attributes.contains(attr)) {
                return true;
            }
            else 
                return false;
                
    }
//		while (it.hasNext()) {
//			NodeAttribute attr = (NodeAttribute) it.next();
//			attrNames.add(attr.getKey());
//		}
        
	//	return attrNames;
	
}
